package vip

import (
	"context"
	vipModel "github.com/beherochuling/beesoft_api/app/model/vip"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gtime"

	"github.com/beherochuling/beesoft_api/app/dao"
	"github.com/beherochuling/beesoft_api/app/model/entity"
)

// # 订单

func NewTransaction() *TransactionService {
	return &TransactionService{}
}

type TransactionService struct {
	unscoped bool
}

func (r *TransactionService) SetUnscoped(unscoped bool) *TransactionService {
	r.unscoped = unscoped

	return r
}

func (r *TransactionService) Scan(ctx context.Context, callback func(model *gdb.Model) *gdb.Model) *entity.VipTransactions {
	var item *entity.VipTransactions

	m := dao.VipTransactions.Ctx(ctx)

	m = callback(m)

	err := m.Scan(&item)
	if err != nil {
		panic(err)
	}

	return item
}
func (r *TransactionService) ScanById(ctx context.Context, id uint) *entity.VipTransactions {
	var item *entity.VipTransactions

	err := dao.VipTransactions.Ctx(ctx).
		WherePri(id).
		Scan(&item)
	if err != nil {
		panic(err)
	}

	return item
}
func (r *TransactionService) ScanRights(ctx context.Context, callback func(model *gdb.Model) *gdb.Model) []entity.VipTransactionRights {
	var items []entity.VipTransactionRights

	m := dao.VipTransactionRights.Ctx(ctx)

	m = callback(m)

	err := m.Scan(&items)
	if err != nil {
		panic(err)
	}

	return items
}
func (r *TransactionService) DestroyById(ctx context.Context, id uint, nextAt *gtime.Time) {
	_, err := dao.VipTransactionRights.Ctx(ctx).
		WherePri(id).
		Update(g.Map{
			dao.VipTransactionRights.Columns().NextAt: nextAt,
		})
	if err != nil {
		panic(err)
	}
}

// # 后台接口

func (r *TransactionService) Transactions(ctx context.Context, req *vipModel.TransactionListRequest) []entity.VipTransactions {
	m := dao.VipTransactions.Ctx(ctx)
	if req.Id != 0 {
		m = m.Where(dao.VipTransactions.Columns().Id, req.Id)
	}
	if req.TransactionId != "" {
		m = m.Where(dao.VipTransactions.Columns().TransactionId, req.TransactionId)
	}
	if req.RelationType != "" {
		m = m.Where(dao.VipTransactions.Columns().RelationType, req.RelationType)
	}
	if req.RelationId != 0 {
		m = m.Where(dao.VipTransactions.Columns().RelationId, req.RelationId)
	}
	// if req.ProductId != 0 {
	// 	m = m.Where(dao.Transactions.Columns().ProductId, req.ProductId)
	// }
	// if req.VipType != "" {
	// 	m = m.Where(dao.Transactions.Columns().Type, VipType.Format(ctx, req.VipType))
	// }
	if r.unscoped {
		m = m.Unscoped()
	}

	var items []entity.VipTransactions
	err := m.OrderDesc(dao.VipTransactions.Columns().Id).
		Page(req.Page, req.PerPage).
		Scan(&items)
	if err != nil {
		panic(err)
	}

	return items
}
func (r *TransactionService) DestroyTransactions(ctx context.Context, id uint) {
	_, err := dao.VipTransactions.Ctx(ctx).
		WherePri(id).
		Delete()
	if err != nil {
		panic(err)
	}
}
func (r *TransactionService) TransactionRights(ctx context.Context, req *vipModel.TransactionRightListRequest) []entity.VipTransactionRights {
	m := dao.VipTransactionRights.Ctx(ctx)
	if req.Id != 0 {
		m = m.Where(dao.VipTransactionRights.Columns().Id, req.Id)
	}
	if req.TransactionId != "" {
		m = m.Where(dao.VipTransactionRights.Columns().TransactionId, req.TransactionId)
	}
	if req.RelationType != "" {
		m = m.Where(dao.VipTransactionRights.Columns().RelationType, req.RelationType)
	}
	if req.RelationId != 0 {
		m = m.Where(dao.VipTransactionRights.Columns().RelationId, req.RelationId)
	}
	// if req.ProductId != 0 {
	// 	m = m.Where(dao.TransactionRights.Columns().ProductId, req.ProductId)
	// }
	// if req.VipType != "" {
	// 	m = m.Where(dao.TransactionRights.Columns().Type, VipType.Format(ctx, req.VipType))
	// }

	var items []entity.VipTransactionRights
	err := m.OrderDesc(dao.VipTransactionRights.Columns().Id).
		Page(req.Page, req.PerPage).
		Scan(&items)
	if err != nil {
		panic(err)
	}

	return items
}
func (r *TransactionService) DestroyTransactionRights(ctx context.Context, id uint) {
	_, err := dao.VipTransactionRights.Ctx(ctx).
		WherePri(id).
		Delete()
	if err != nil {
		panic(err)
	}
}
